package Presentacion;

import Clases.Asignarprestamo;
import Clases.AsignarprestamoId;
import Clases.Empleado;
import Clases.Pago;
import Clases.PagoId;
import Clases.Prestamo;
import Control.ControlEmpleado;
import Control.ControlPago;
import Control.ControlPrestamo;
import de.javasoft.plaf.synthetica.SyntheticaLookAndFeel;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.NumberFormatter;

public class ManejoPrestamos extends javax.swing.JFrame {

    ControlPrestamo controlPrestamo = ControlPrestamo.obtenerInstancia();
    ControlPago controlP = ControlPago.obtenerInstancia();
    ControlEmpleado controlE = ControlEmpleado.obtenerInstancia();
    private ImageIcon iconoError;
    NumberFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
    private static ManejoPrestamos instancia = null;

    private ManejoPrestamos() {
        initComponents();
        inicioTooltops();
    }

    private void inicioTooltops() {
        iconoError = new ImageIcon(getClass().getResource("/Imagenes/warning.png"));

        labelMonto.setIcon(iconoError);
        labelMonto.setToolTipText("Debe escribir un monto válido.");

        labelQuincena.setIcon(iconoError);
        labelQuincena.setToolTipText("Debe escribir una quincena válido.");

        jLabelInteres.setIcon(iconoError);
        jLabelInteres.setToolTipText("Debe escribir un interés válido.");

        jLabelEmpleado.setIcon(iconoError);
        jLabelEmpleado.setToolTipText("Debe buscar un empleado primero");

        ocultarLabels();
    }

    private void ocultarLabels() {

        labelMonto.setVisible(false);
        jLabelEmpleado.setVisible(false);
        jLabelInteres.setVisible(false);
        labelQuincena.setVisible(false);
    }

    public void init() {
        setTitle(" Manejo de Préstamos");
        setLocationRelativeTo(null);
        UIManager.removeAuxiliaryLookAndFeel(UIManager.getLookAndFeel());
        SwingUtilities.updateComponentTreeUI(this);
        try {
            UIManager.removeAuxiliaryLookAndFeel(UIManager.getLookAndFeel());
            SyntheticaLookAndFeel.setWindowsDecorated(false);
            UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);

        } catch (Exception ex) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        jTable4.getColumnModel().getColumn(0).setWidth(jTable4.getColumnModel().getColumn(0).getMinWidth() + 10);
        jTable4.getColumnModel().getColumn(0).setMaxWidth(jTable4.getColumnModel().getColumn(0).getMinWidth() + 10);
        this.NumberFormat();
        this.limpiar();
        this.setVisible(true);
    }

    public void NumberFormat() {
        numberFormat.setMaximumIntegerDigits(7);
        numberFormat.setMinimumFractionDigits(0);
        numberFormat.setMinimumIntegerDigits(2);
        NumberFormatter nfr = new NumberFormatter(numberFormat);
        this.jTextField3.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(nfr));
        this.jTextField4.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(nfr));
    }

    public static ManejoPrestamos obtenerInstancia() {
        if (instancia == null) {
            instancia = new ManejoPrestamos();
        }
        return instancia;
    }

    public static boolean esNumero(String str) {
        for (char c : str.toCharArray()) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        return true;
    }

    public void llenarTablaAsignacionesPrestamo() {
        String cedulaEmpleado = labelCedulaConsultar.getText();
        Empleado empleado = controlE.getEmpleadoCedula(Long.valueOf(cedulaEmpleado));

        ArrayList<java.math.BigInteger> asignaciones = controlPrestamo.getAsignacionPrestamo(empleado.getNumEmpleado());
        DefaultTableModel m = (DefaultTableModel) jTable4.getModel();
        try {
            while (m.getRowCount() > 0) {
                m.removeRow(0);
            }
            for (int i = 0; i < asignaciones.size(); i++) {
                Prestamo prestamo = controlPrestamo.getPrestamoPorCodigo(asignaciones.get(i).intValue());
                m.insertRow(m.getRowCount(), new Object[]{new Boolean(false), prestamo.getCodigo(), prestamo.getMontoTotal(), prestamo.getMontoPago(), prestamo.getDeuda(), prestamo.getInteres(), prestamo.getCuota()});
            }
        } catch (Exception e) {
            System.out.println(e.getCause());
            System.out.println(e.getMessage());
        }

    }

    public ArrayList<Integer> getPrestamosSeleccionados() {
        ArrayList<Integer> prestamos = new ArrayList<Integer>();
        for (int i = 0; i < jTable4.getRowCount(); i++) {
            if ((Boolean) jTable4.getValueAt(i, 0).equals(Boolean.TRUE)) {
                prestamos.add((Integer) jTable4.getValueAt(i, 1));
            }
        }
        return prestamos;
    }
    void crear(){
    
    try {
            ocultarLabels();
            String cedEmpleado = labelCedulaCrear.getText();
            String monto = jTextField3.getText();
            String cuota = jTextField4.getText();
            String interes = jTextField5.getText();
            int error = 0;
            double valorMonto = 0;
            double valorCuota = 0;

            ///////////////////////////VALIDACIONES//////////////////////////
            if (interes.equals("")) {
                interes = "0";
            } else if (!esNumero(interes)) {
                jLabelInteres.setVisible(true);
                error = 1;
            }
            if (Character.isDigit(cedEmpleado.charAt(0)) == false) {
                jLabelEmpleado.setVisible(true);
                error = 1;
            }
            if (monto.equals("")) {
                labelMonto.setVisible(true);
                error = 1;
            } else {
                valorMonto = jTextField3.getText().trim().isEmpty() ? null : numberFormat.parse(jTextField3.getText()).doubleValue();
                if (valorMonto < 1000) {
                    labelMonto.setVisible(true);
                    error = 1;
                }

            }

            if (cuota.equals("")) {
                labelQuincena.setVisible(true);
                error = 1;
            } else {
                valorCuota = jTextField4.getText().trim().isEmpty() ? null : numberFormat.parse(jTextField4.getText()).doubleValue();
                if (valorCuota < 1000 || valorCuota > valorMonto) {
                    labelQuincena.setVisible(true);
                    error = 1;
                }
            }
            //////////////////// FIN VALIDACIONES ////////////////////////

            if (error == 0) {
                ArrayList<Date> fechas = controlP.getPago();

                if (!monto.equals("") && !cuota.equals("")) {
                    Prestamo prestamo = null;
                    if (!interes.equals("")) {
                        Double montoTotal = valorMonto + (valorMonto * (Integer.valueOf(interes) / 100));
                        prestamo = new Prestamo(montoTotal, valorCuota, montoTotal-valorCuota, Integer.valueOf(interes), valorCuota, true);
                    } else {
                        prestamo = new Prestamo(valorMonto,valorCuota, valorMonto-valorCuota, 0, valorCuota, true);
                    }
                    int codigoPrestamo = controlPrestamo.insertarPrestamo(prestamo);
                    if (codigoPrestamo == 0) {
                        JOptionPane.showMessageDialog(null, "Problema al crear el préstamo", "Mensaje", JOptionPane.ERROR_MESSAGE);
                    } else {
                        Empleado empleado = controlE.getEmpleadoCedula(Long.valueOf(cedEmpleado));
                        PagoId pagoId = new PagoId(empleado.getNumEmpleado(), fechas.get(0), fechas.get(1));
                        Pago pago = new Pago(pagoId, empleado);

                        AsignarprestamoId prestamoId = new AsignarprestamoId(empleado.getNumEmpleado(), codigoPrestamo, fechas.get(0), fechas.get(1));
                        Asignarprestamo asignacion = new Asignarprestamo(prestamoId, prestamo, pago, prestamo.getDeuda(), prestamo.getMontoPago());
                        if (controlPrestamo.asignarPrestamoEmpleado(asignacion)) {
                            JOptionPane.showMessageDialog(null, "Se creó el préstamo correctamente ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                        } else {

                            prestamo.setCodigo(codigoPrestamo);
                            controlPrestamo.eliminarPrestamo(prestamo);
                            JOptionPane.showMessageDialog(null, "Problema al crear el préstamo", "Mensaje", JOptionPane.ERROR_MESSAGE);
                        }

                        this.limpiarIngresar();
                    }

                    this.limpiarIngresar();
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(ManejoPrestamos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jButton8 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable4 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        labelCedulaConsultar = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        labelCodigoConsultar = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        labelNombreConsultar = new javax.swing.JLabel();
        labelInteres = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton9 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jTextField5 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jTextField3 = new javax.swing.JFormattedTextField();
        jTextField4 = new javax.swing.JFormattedTextField();
        labelMonto = new javax.swing.JLabel();
        labelQuincena = new javax.swing.JLabel();
        jLabelInteres = new javax.swing.JLabel();
        jLabelEmpleado = new javax.swing.JLabel();
        labelCedulaCrear = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        Nombre = new javax.swing.JLabel();
        labelCodigoCrear = new javax.swing.JLabel();
        labelNombreCrear = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(680, 420));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Préstamo");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Cédula");

        jButton8.setText("Buscar Empleado");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jTable4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "", "Código", "Monto Total", "Monto Pago", "Deuda", "Interes", "Cuota"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Integer.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(jTable4);

        jButton1.setText("Eliminar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        labelCedulaConsultar.setText("__________________");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setText("Código:");

        labelCodigoConsultar.setText("__________________");

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel12.setText("Nombre:");

        labelNombreConsultar.setText("__________________");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane4)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel12)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(labelNombreConsultar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel11)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(labelCodigoConsultar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(labelCedulaConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(34, 34, 34)
                                .addComponent(jButton8)
                                .addGap(127, 292, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jButton8)
                    .addComponent(labelCedulaConsultar))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(labelCodigoConsultar))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(labelNombreConsultar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Consultar Préstamo", jPanel1);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Ingrese los datos del préstamo: ");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Cédula");

        jButton9.setText("Buscar Empleado");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Monto Total:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Cuota Quincenal:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Interés:");

        jButton6.setText("Crear");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jTextField5.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField5KeyPressed(evt);
            }
        });

        jLabel6.setText("%");

        jButton7.setText("Salir");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        labelMonto.setText("x");
        labelMonto.setMaximumSize(new java.awt.Dimension(24, 24));
        labelMonto.setMinimumSize(new java.awt.Dimension(22, 22));
        labelMonto.setPreferredSize(new java.awt.Dimension(22, 22));

        labelQuincena.setText("x");
        labelQuincena.setMaximumSize(new java.awt.Dimension(24, 24));
        labelQuincena.setMinimumSize(new java.awt.Dimension(22, 22));
        labelQuincena.setPreferredSize(new java.awt.Dimension(22, 22));

        jLabelInteres.setText("x");
        jLabelInteres.setMaximumSize(null);
        jLabelInteres.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelInteres.setPreferredSize(new java.awt.Dimension(22, 22));

        jLabelEmpleado.setText("x");
        jLabelEmpleado.setMaximumSize(new java.awt.Dimension(24, 24));
        jLabelEmpleado.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelEmpleado.setPreferredSize(new java.awt.Dimension(22, 22));

        labelCedulaCrear.setText("____________________");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Código:");

        Nombre.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        Nombre.setText("Nombre:");

        labelCodigoCrear.setText("____________________");

        labelNombreCrear.setText("______________________________________");

        javax.swing.GroupLayout labelInteresLayout = new javax.swing.GroupLayout(labelInteres);
        labelInteres.setLayout(labelInteresLayout);
        labelInteresLayout.setHorizontalGroup(
            labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, labelInteresLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButton6)
                .addGap(18, 18, 18)
                .addComponent(jButton7)
                .addGap(130, 130, 130))
            .addGroup(labelInteresLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(labelInteresLayout.createSequentialGroup()
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(19, 19, 19)
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(labelInteresLayout.createSequentialGroup()
                                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelInteres, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(labelInteresLayout.createSequentialGroup()
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(labelQuincena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(labelInteresLayout.createSequentialGroup()
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(labelMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(labelInteresLayout.createSequentialGroup()
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, labelInteresLayout.createSequentialGroup()
                                .addComponent(Nombre)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(labelNombreCrear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, labelInteresLayout.createSequentialGroup()
                                .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, labelInteresLayout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(labelCodigoCrear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(labelInteresLayout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(labelCedulaCrear)))
                                .addGap(18, 18, 18)
                                .addComponent(jButton9)))
                        .addGap(18, 18, 18)
                        .addComponent(jLabelEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(302, Short.MAX_VALUE))
        );
        labelInteresLayout.setVerticalGroup(
            labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(labelInteresLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jButton9)
                    .addComponent(jLabelEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCedulaCrear))
                .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(labelInteresLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(labelCodigoCrear))
                        .addGap(18, 18, 18)
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Nombre)
                            .addComponent(labelNombreCrear))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelQuincena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabelInteres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(67, 67, 67))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, labelInteresLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(labelInteresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton6)
                            .addComponent(jButton7))
                        .addContainerGap())))
        );

        jTabbedPane1.addTab("Crear Préstamo", labelInteres);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(250, 250, 250)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

        Empleados empleado = new Empleados();
        empleado.init("consultarPrestamo");
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ArrayList<Integer> seleccionados = this.getPrestamosSeleccionados();
        boolean elimino = true;
        ArrayList<Date> fechas = controlP.getPago();
        if (seleccionados.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Seleccione al menos un préstamo", "Mensaje", JOptionPane.ERROR_MESSAGE);
        } else {
            int confirmado = JOptionPane.showConfirmDialog(null, "Está seguro que quiere eliminar el/los préstamo(s)?");
            if (JOptionPane.OK_OPTION == confirmado) {
                for (int i = 0; i < seleccionados.size(); i++) {
                    /* ESTO ELIMINA PRESTAMO POR COMPLETO (CN SUS ASIGNACIONES)
                     Prestamo prestamo = controlPrestamo.getPrestamoPorCodigo(seleccionados.get(i));
                     Empleado empleado = controlE.getEmpleadoCedula(Long.valueOf(jTextField1.getText()));
                     PagoId pagoId = new PagoId(empleado.getNumEmpleado(), fechas.get(0), fechas.get(1));
                     Pago pago = new Pago(pagoId, empleado);
                     AsignarprestamoId prestamoId = new AsignarprestamoId(empleado.getNumEmpleado(), prestamo.getCodigo(), fechas.get(0), fechas.get(1));
                     Asignarprestamo asignacion = new Asignarprestamo(prestamoId, prestamo, pago, 0.0, 0.0);
                     controlPrestamo.desasignarPrestamo(asignacion);
                     elimino = controlPrestamo.eliminarPrestamo(prestamo);*/
                    Prestamo p = controlPrestamo.getPrestamoPorCodigo(seleccionados.get(i));
                    p.setActivo(false);
                    if (!controlPrestamo.desactivarAsignacionPrestamo(p)) {
                        elimino = false;
                    }

                }
                this.llenarTablaAsignacionesPrestamo();
            }
            if (elimino) {
                JOptionPane.showMessageDialog(null, "Eliminación exitosa", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                llenarTablaAsignacionesPrestamo();
            } else {
                JOptionPane.showMessageDialog(null, "No se pudo eliminar el préstamo o algunos préstamos", "Mensaje", JOptionPane.ERROR_MESSAGE);
            }
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.limpiar();
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        Empleados empleado = new Empleados();
        empleado.init("prestamo");
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        this.crear();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        this.limpiar();
        dispose();
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jTextField5KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField5KeyPressed
          int key = evt.getKeyCode();
        if (key == evt.VK_ENTER) {
            this.crear();
        }
    }//GEN-LAST:event_jTextField5KeyPressed

    public void limpiar() {
        labelCedulaConsultar.setText("__________________");
        labelCodigoConsultar.setText("__________________");
        labelNombreConsultar.setText("__________________");
        this.limpiarIngresar();
        DefaultTableModel m = (DefaultTableModel) jTable4.getModel();
         while (m.getRowCount() > 0) {
                m.removeRow(0);
            }

    }

    public void setjTextFieldsConsultar(String cedEmpleado) {
        this.labelCedulaConsultar.setText(cedEmpleado);
        ControlEmpleado controlE = ControlEmpleado.obtenerInstancia();
        Empleado e = controlE.getEmpleadoCedula(Long.parseLong(cedEmpleado));
        labelCodigoConsultar.setText(String.valueOf(e.getCodigo()));
        labelNombreConsultar.setText(e.getNombre() + " " + e.getApellido1());

    }

    public void setjTextFieldsCrear(String cedEmpleado) {
        this.labelCedulaCrear.setText(cedEmpleado);
        ControlEmpleado controlE = ControlEmpleado.obtenerInstancia();
        Empleado e = controlE.getEmpleadoCedula(Long.parseLong(cedEmpleado));
        labelCodigoCrear.setText(String.valueOf(e.getCodigo()));
        labelNombreCrear.setText(e.getNombre() + " " + e.getApellido1());
    }

    public void limpiarIngresar() {

        labelCedulaCrear.setText("_________________");
        labelCodigoCrear.setText("_________________");
        labelNombreCrear.setText("__________________________________");
        /* labelCedulaConsultar.setText("________________");
         labelCodigoConsultar.setText("________________");
         labelNombreConsultar.setText("______________________________");*/
        jTextField3.setText("");
        jTextField3.setValue(null);
        jTextField4.setText("");
        jTextField4.setValue(null);
        jTextField5.setText("");
        this.ocultarLabels();
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ManejoPrestamos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ManejoPrestamos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ManejoPrestamos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ManejoPrestamos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ManejoPrestamos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Nombre;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelEmpleado;
    private javax.swing.JLabel jLabelInteres;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable4;
    private javax.swing.JFormattedTextField jTextField3;
    private javax.swing.JFormattedTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JLabel labelCedulaConsultar;
    private javax.swing.JLabel labelCedulaCrear;
    private javax.swing.JLabel labelCodigoConsultar;
    private javax.swing.JLabel labelCodigoCrear;
    private javax.swing.JPanel labelInteres;
    private javax.swing.JLabel labelMonto;
    private javax.swing.JLabel labelNombreConsultar;
    private javax.swing.JLabel labelNombreCrear;
    private javax.swing.JLabel labelQuincena;
    // End of variables declaration//GEN-END:variables
}
